<?php
add_action('widgets_init', 'bhutanese_refugees_load_menubar_widgets');

/**
 * Register our widget.
 * 'Example_Widget' is the widget class used below.
 *
 * @since 0.1
 */
function bhutanese_refugees_load_menubar_widgets() {
    register_widget('Bhutanese_Refugees_Menubar_Widget');
}

/**
 * Example Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Bhutanese_Refugees_Menubar_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function Bhutanese_Refugees_Menubar_Widget() {
        /* Widget settings. */
        $widget_ops = array('classname' => 'bhutanese_refugees', 'description' => __('Widgets for displaying tree view menubar', 'bhutanese_refugees'));

        /* Widget control settings. */
        $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'bhutanese-refugees-menubar-widgets');

        /* Create the widget. */
        $this->WP_Widget('bhutanese-refugees-menubar-widgets', __('Bhutanese rufugees menubar widget', 'bhutanese_refugees'), $widget_ops, $control_ops);
    }

    /**
     * How to display the widget on the screen.
     */
    function widget($args, $instance) {
        extract($args);
        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', empty($instance['title']) ? __('') : $instance['title']);
        $menu = $instance['selectMenu'];

        echo $before_widget;
        if($title!=""){
        echo $before_title;
        echo $title;
        echo $after_title;
        }
        bhutanese_refugees_main_nav_tree($menu);
        /* After widget (defined by themes). */
        echo $after_widget;
    }

    /**
     * Update the widget settings.
     */
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        /* Strip tags for post_title,post_no_show,categories_title,comment_title and comment_no_show to remove HTML (important for text inputs). */
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['selectMenu'] = strip_tags($new_instance['selectMenu']);
        return $instance;
    }

    /**
     * Displays the widget settings controls on the widget panel.
     * Make use of the get_field_id() and get_field_name() function
     * when creating your form elements. This handles the confusing stuff.
     */
    function form($instance) {
//                wp_deregister_script( 'jquery' );
//                wp_register_script( 'jquery', 'http://code.jquery.com/jquery-latest.pack.js', false, '' );
//                add_action('wp_print_scripts',  wp_enqueue_script('jquery'));
        /* Set up some default widget settings. */
        $defaults = array();
        $instance = wp_parse_args((array) $instance, $defaults);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bhutanese_refugees'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" type="text"/>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('selectMenu'); ?>">Style Menu:</label>
            <?php
            $menu_list = get_terms('nav_menu');
            if (count($menu_list) > 0) {
                ?>
                <select name="<?php echo $this->get_field_name('selectMenu'); ?>" id="<?php echo $this->get_field_id('selectMenu'); ?>">
                    <?php
                    foreach ($menu_list as $menu) {
                        $value = $menu->term_id;
                        $name = $menu->name;
                        $selected = ($value == $instance['selectMenu']) ? "selected='selected'" : "";
                        ?>
                        <option value="<?php echo $value; ?>" <?php echo $selected; ?>><?php echo $name; ?></option>
                    <?php } ?>
                </select>
            <?php
            } else {
                echo "No Menu found. Please create menu[Appearance&gt;&gt;menus] to use this widget.";
            }
            ?>
        </p>
        <?php
    }

}
?>